package com.tech15.core.action;

import java.util.Map;

import javacommon.base.BaseStruts2Action;
import javacommon.util.DateTools;
import javacommon.util.LoginInfo;
import javacommon.util.Notes;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import cn.org.rapid_framework.page.Page;

import com.tech15.core.service.ModuleManager;
import com.tech15.pub.service.AttendanceManager;
import com.tech15.pub.service.LeaderWorkstateManager;
import com.tech15.pub.service.PubAppsManager;
import com.tech15.pub.service.PubCcsqManager;
import com.tech15.pub.service.PubInfoManager;
import com.tech15.pub.service.PubOfficeManager;
import com.tech15.pub.service.PubSoftManager;
import com.tech15.pub.vo.query.LeaderWorkstateQuery;
import com.tech15.pub.vo.query.PubAppsQuery;
import com.tech15.pub.vo.query.PubInfoQuery;
import com.tech15.pub.vo.query.PubOfficeQuery;
import com.tech15.task.service.TaskManager;
import com.tech15.task.service.TaskReturnManager;
import com.tech15.task.service.ZdxzdcManager;

public class IndexAction extends BaseStruts2Action{
	protected static final String LOGOUT_JSP = "/logout.html";
	protected static final String LOGIN_JSP = "/login.jsp";
	protected static final String HOME_JSP = "/index.jsp";
	protected static final String ADMIN_JSP = "/portal_index.jsp";
	protected static final String LOGOUT_ACTION = "!/member_forward.do";
	
	private ModuleManager moduleManager;
	private PubAppsManager pubAppsManager;
	private PubInfoManager pubInfoManager;
	private PubOfficeManager pubOfficeManager;
	private TaskManager taskManager;
	private AttendanceManager attendanceManager;
	private LeaderWorkstateManager leaderWorkstateManager;
	private ZdxzdcManager zdxzdcManager;
	private TaskReturnManager taskReturnManager;
	private PubCcsqManager pubCcsqManager;
	
	public void setTaskReturnManager(TaskReturnManager manager) {
		this.taskReturnManager = manager;
	}
	
	public void setZdxzdcManager(ZdxzdcManager manager) {
		this.zdxzdcManager = manager;
	}
	
	public void setLeaderWorkstateManager(LeaderWorkstateManager manager) {
		this.leaderWorkstateManager = manager;
	}
	
	public void setAttendanceManager(AttendanceManager manager) {
		this.attendanceManager = manager;
	}
	
	public void setTaskManager(TaskManager manager) {
		this.taskManager = manager;
	}
	
	public void setModuleManager(ModuleManager moduleManager) {
		this.moduleManager = moduleManager;
	}
	public void setPubAppsManager(PubAppsManager manager) {
		this.pubAppsManager = manager;
	}
	public void setPubInfoManager(PubInfoManager manager) {
		this.pubInfoManager = manager;
	}
	public void setPubOfficeManager(PubOfficeManager manager) {
		this.pubOfficeManager = manager;
	}
	public void setPubCcsqManager(PubCcsqManager manager) {
		this.pubCcsqManager = manager;
	}
	
	@Override
	public String execute() throws Exception {
		LoginInfo loginInfo = this.getLoginInfo();
		if(loginInfo == null){
			return LOGOUT_ACTION;
		}else{
			//通知公告
			PubInfoQuery infoquery = newQuery(PubInfoQuery.class,"istop desc,fbsj desc");
			infoquery.setZt(new Long(9));
			infoquery.setUserid(getLoginInfo().getUserid());
			Page pageInfo = pubInfoManager.findPage(infoquery);
			getRequest().setAttribute("infoList", pageInfo.getResult());
			
			//外部应用系统接口
			PubAppsQuery app0query = newQuery(PubAppsQuery.class,"appsort asc");
			app0query.setCategory(0);
			app0query.setIsshfw(0);
			Page pageApp0 = pubAppsManager.findPage(app0query);
			getRequest().setAttribute("app0List", pageApp0.getResult());
			
			//子系统接口
			PubAppsQuery app1query = newQuery(PubAppsQuery.class,"appsort asc");
			app1query.setCategory(1);
			app1query.setIssys(0);
			app1query.setIsshfw(0);
			Page pageApp1 = pubAppsManager.findPage(app1query);
			getRequest().setAttribute("app1List", pageApp1.getResult());
			
			/*//规章制度
			PubInfoQuery gzzdinfoquery = newQuery(PubInfoQuery.class,"fbsj desc");
			gzzdinfoquery.setZt(new Long(58));
			Page gzzdpageInfo = pubInfoManager.findPage(gzzdinfoquery);
			getRequest().setAttribute("gzzdList", gzzdpageInfo.getResult());*/
			
			PubOfficeQuery zbybquery = newQuery(PubOfficeQuery.class,"uptime desc");
			/*zbybquery.setLb(3);
			zbybquery.setLb2(2);*/
			zbybquery.setUserid(getLoginInfo().getUserid());
			Page gzzdpageInfo = pubOfficeManager.searchIndex(zbybquery);
			getRequest().setAttribute("zbybList", gzzdpageInfo.getResult());
			
			
			LeaderWorkstateQuery leaderquery = newQuery(LeaderWorkstateQuery.class,"sortno asc");
			
			Page leader = leaderWorkstateManager.findPage(leaderquery);
			getRequest().setAttribute("leaderList", leader.getResult());
			
			Integer userCtg = loginInfo.getCategory();
			if(userCtg == 3){
				Integer wclrw = taskManager.getWclTask(getLoginInfo().getUserid());
				Integer xzdcCount = zdxzdcManager.myTaskCount(getLoginInfo().getUserid());
				Integer taskReturnCount = taskReturnManager.myReturnCount(getLoginInfo().getUserid());
				Integer ccsqCount = pubCcsqManager.getTaskCount(getLoginInfo().getUserid());
				
				getRequest().setAttribute("wclrw", wclrw+xzdcCount+taskReturnCount+ccsqCount);
			
				Integer yclrw = taskManager.getYclTask(getLoginInfo().getUserid());
				getRequest().setAttribute("yclrw", yclrw);
			}
			
			Integer isQd = attendanceManager.getIfqd(getLoginInfo().getUserid(), DateTools.getDisplayDate());
			getRequest().setAttribute("isqd", isQd);
			
			Integer qdzs = attendanceManager.getQdZs(getLoginInfo().getUserid(), DateTools.getDisplayDate("yyyy-MM"));
			getRequest().setAttribute("qdzs", qdzs);
		}
		return HOME_JSP;
	}
	
	public String admin() throws Exception {
		LoginInfo loginInfo = this.getLoginInfo();
		if(loginInfo == null){
			return LOGOUT_ACTION;
		}else{
			Map map = moduleManager.findAllFixFunByUserId(loginInfo.getUserid(),Notes.APPID);
			JsonConfig jsonConfig = new JsonConfig();
			JSON json = JSONSerializer.toJSON(map, jsonConfig);  
			//System.out.println(json.toString());
			getRequest().setAttribute("fixedFunList", json.toString());
			
		}
		return ADMIN_JSP;
	}
	
	public String apps() throws Exception {
		LoginInfo loginInfo = this.getLoginInfo();
		if(loginInfo == null){
			return LOGOUT_ACTION;
		}else{
			if(isNullOrEmptyString(appid)){
				appid = Notes.APPID;
			}
			Map map = moduleManager.findAllFixFunByUserId(loginInfo.getUserid(),appid);
			JsonConfig jsonConfig = new JsonConfig();
			JSON json = JSONSerializer.toJSON(map, jsonConfig);  
			//System.out.println(json.toString());
			getRequest().setAttribute("fixedFunList", json.toString());
			
		}
		return ADMIN_JSP;
	}
	
	private Integer appid;

	public Integer getAppid() {
		return appid;
	}

	public void setAppid(Integer appid) {
		this.appid = appid;
	}
	
	
}
